package com.akuhome.resource.controller;

import com.akuhome.resource.service.QuartzService;
import com.akuhome.resource.util.QuartzUtil;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 注意:生产环境中被禁用!!!
 */
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    Logger logger = LoggerFactory.getLogger(QuartzController.class);
    @Autowired
    QuartzService quartzService;
    @Autowired
    Scheduler scheduler;
//    @PostMapping
//    public Object test(){
//        Project project = new Project();
//        project.setUid("202204041708415062");
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(2022,Calendar.APRIL,5);
//        Date date = calendar.getTime();
//        project.setStartDate(date);
//        quartzService.addUpdateProjectStatusJob(project);
////        quartzService.addAddCheckJob(project);
//        return null;
//    }

    @PutMapping
    public Object trigger(String name){
        QuartzUtil.runOnce(scheduler,name);
        return null;
    }

    @GetMapping
    public Object getJobSize() throws SchedulerException {
        Set<JobKey> jobKeyList = scheduler.getJobKeys(GroupMatcher.anyJobGroup());
        JobKey[] jobKeys = jobKeyList.toArray(new JobKey[0]);
        logger.info("现在运行的任务数量:"+jobKeyList.size());
        for(JobKey key : jobKeys){
            logger.info("现在运行的任务:"+key.getName());
        }
        return null;
    }
}
